﻿
@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link href="~/layui-v2.6.8/layui-v2.6.8/layui/css/layui.css" rel="stylesheet" />
    <script src="~/lib/jquery/dist/jquery.js"></script>
    <script src="~/layui-v2.6.8/layui-v2.6.8/layui/layui.js"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
    <div class="demoTable">
        查询：
        <div class="layui-inline">
            <input class="layui-input" name="roleName" id="demoReload" autocomplete="off">
        </div>
        <button class="layui-btn" data-type="reload">搜索</button>
        <button class="layui-btn" data-type="add">添加</button>
        <button class="layui-btn" data-type="batchDel">批量删除</button>
    </div>

    <table class="layui-hide" id="test" lay-filter="test"></table>

    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="allocation">分配权限</a>

    </script>


    <script src="//res.layui.com/layui/dist/layui.js" charset="utf-8"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->

    <script>

        //（4） 前端：浏览器请求服务器的时候，要带上JWT
        $.ajaxSetup({
            beforeSend: function (xhr) {
                if (localStorage.getItem("token") !== null) {
                    //（4） 前端：浏览器请求服务器的时候，要带上JWT
                    xhr.setRequestHeader('Authorization', 'Bearer ' + localStorage.getItem("token"));
                }
            }
        });

        layui.use('table', function () {

            var table = layui.table;

            var Hob= table.render({
                elem: '#test'
                , url: 'https://localhost:44398/api/RolesAPI/GetShow'
                , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                    title: '提示'
                    , layEvent: 'LAYTABLE_TIPS'
                    , icon: 'layui-icon-tips'
                }]
                , title: '用户数据表'
                , cols: [[
                    { type: 'checkbox', fixed: 'left' }
                    , { field: 'roleID', title: '角色ID', width: 120, fixed: 'left', unresize: true, sort: true }
                    , { field: 'roleName', title: '角色名称', width: 120, edit: 'text' }
                    , { field: 'sort', title: '显示顺序', width: 120, edit: 'text', sort: true }
                    , { field: 'remark', title: '备注', width: 120 }
                    , { fixed: 'right', title: '操作', toolbar: '#barDemo', width: 300 }
                ]]
                , page: true
            });

            //监听行工具事件
            table.on('tool(test)', function (obj) {
                var data = obj.data;
                //console.log(obj)
                if (obj.event === 'del') {
                    layer.confirm('真的删除行么', function (index) {
                        $.ajax({
                            url: 'https://localhost:44398/api/RolesAPI/Delete?id=' + data.roleID,
                            type: 'post',
                            success: function (d) {
                                layer.msg("删除成功!", function () {
                                    Hob.reload();
                                })
                            }
                        })
                    });
                } else if (obj.event === 'edit') {
                    layer.open({
                        type: 2,
                        area: ['700px', '500px'],
                        fixed: false, //不固定
                        maxmin: true,
                        content: '/RolesWeb/Fill?id=' + data.roleID,
                    });
                }
                else if (obj.event === 'allocation') {
                    layer.open({
                        type: 2,
                        area: ['700px', '500px'],
                        fixed: false, //不固定
                        maxmin: true,
                        content: '/RolesWeb/ShuShow?roleID=' + data.roleID
                    });
                } else if (obj.event === 'addRoleMenus') {
                    layer.open({
                        type: 2,
                        area: ['700px', '500px'],
                        fixed: false, //不固定
                        maxmin: true,
                        content: '/RolesWeb/AddRoleTree?roleID=' + data.roleID
                    });
                }
            });
            var $ = layui.$, active = { //查询
                reload: function () {
                    var demoReload = $('#demoReload');

                    //执行重载
                    table.reload('test', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        , where: {
                            roleName: demoReload.val()
                        }
                    });
                },
                add: function () {  //添加的弹框
                    layer.open({
                        type: 2,
                        area: ['700px', '500px'],
                        fixed: false, //不固定
                        maxmin: true,
                        content: '/RolesWeb/Create'
                    });
                },
                batchDel: function () {

                }
            };

            $('.demoTable .layui-btn').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
        });
    </script>

</body>
</html>
